<template>
	<view class="security-wp">
		<form @submit="formSubmit">
			<view class="c_userinfolistitem title">
				手机号
			</view>
			<view class="c_userinfolist">
				<input disabled="true" type="text" :value="infoMemberMobile" />
			</view>
			<view class="c_userinfolistitem title">
				验证码
			</view>
			<view class="c_userinfolist">
				<input type="text" name="val1" @input="change(0)" placeholder="请输入验证码" />
				<view class="c_userinfolistitem validation" @click="validation">
					{{time}}
				</view>
			</view>
			<view class="prompt" v-show="bol[0]">
				请填写验证码
			</view>
			<view class="c_userinfolistitem title">
				更改手机号
			</view>
			<view class="c_userinfolist">
				<input type="number" name="val2" @input="change(1)" placeholder="请输入新手机号" />
			</view>
			<view class="prompt" v-show="bol[1]">
				请正确填写11位新手机号
			</view>
			<button class="btn" form-type="submit">提交</button>
			<uni-popup ref="bind" type="dialog" :is-mask-click="false">
				<view class="nobind">
					<view>您还未绑定手机号</view>
					<button class="l_w_login" open-type="getPhoneNumber" @getphonenumber="getUserProfile">绑定手机号</button>
				</view>
			</uni-popup>
			
			<uni-popup ref="inputDialog" type="dialog">
				<uni-popup-dialog ref="inputClose" :value="mid" before-close="true" mode="input" title="推荐人ID" placeholder="请输入推荐人ID" @confirm="dialogInputConfirm" @close="closeDialog"></uni-popup-dialog>
			</uni-popup>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				info: [],
				infoMemberMobile: '',
				bol: [false, false, false],
				time: '发送验证码',
			}
		},
		onLoad() {
			this.getInfo();
		},
		methods: {
			getInfo(){
				var that = this;
				that.$axios('Info/member_info', "POST", 'member').then(res => {
					if (res.data.code = 200) {
						that.info = res.data.data;
						that.infoMemberMobile = that.info.member.mobile;
						if(that.info.member.mobile==''||that.info.member.mobile.length<11||that.info.member.mobile===null||that.info.member.mobile==undefined){
							that.$refs.bind.open();
						}
					}
				})
			},
			formSubmit(e) {
				var that = this;
				var val = e.detail.value;
				if (val.val1 == '') {
					this.$set(this.bol, 0, true);
				} else {
					this.$set(this.bol, 0, false);
					if (val.val2.length != 11) {
						this.$set(this.bol, 1, true);
					} else {
						this.$set(this.bol, 1, false);
						if (val.val2 != that.infoMemberMobile) {
							that.$axios('Editmobile/main', "POST", 'member', {
								mobile: that.infoMemberMobile,
								code: val.val1,
								newmobile: val.val2
							}).then(res => {
								if (res.data.code == 200) {
									uni.showModal({
										title: '修改成功！',
										showCancel: false,
										success() {
											uni.switchTab({
												url: '/pages/individualism/user'
											})
										}
									})
								} else {
									uni.showToast({
										title: res.data.data,
										icon: 'none',
										duration: 2000
									});
								}
							})
						} else {
							uni.showToast({
								title: '两次手机号相同，请重新填写！',
								icon: 'none',
								duration: 3500
							});
						}
					}
				}
			},
			validation() {
				var that = this;
				if (that.time != '发送验证码') {
					return;
				}
				that.$axios('Sendcode/bindmobilecode', "POST", 'member', {
					mobile: that.infoMemberMobile
				}).then(res => {
					if (res.data.code = 200) {
						uni.showToast({
							title: '发送成功!'
						})
						that.time = '60s';
						var time = 60000;
						var ss = setInterval(() => {
							time -= 1000;
							if (time <= 0) {
								that.time = '发送验证码';
								clearInterval(ss);
							} else {
								that.time = time / 1000 + 's';
							}
						}, 1000)
					}
				})
			},
			change(e) {
				e == 1 && this.$set(this.bol, e, false);
				e == 0 && this.$set(this.bol, e, false);
			},

			getUserProfile(e) {
				let that = this;
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes);
						let obj = e.detail;
						obj.code1 = loginRes.code;
						that.$axios('bindmobile/bmobile', "POST", 'member', obj).then(res => {
							if (res.data.code == 200) {
								that.$refs.bind.close();
								that.infoMemberMobile = res.data.data.moblie;
							}else if(res.data.code==40001){
								that.$refs.inputDialog.open();
							}
						})
					},
					fail() {
						that.$api.msg('登录失败，请重试！')
					}
				})
				// #endif
				// #ifdef MP-ALIPAY
				my.getPhoneNumber({
					success: (res) => {
						let encryptedData = res.response;
						that.$axios('bindmobile/bmobilealipay', "POST", 'member', {
							code: JSON.parse(encryptedData).response
						}).then(res => {
							if (res.data.code == 200) {
								if (res.data.code == 200) {
									that.$refs.bind.close();
									that.infoMemberMobile = res.data.data.moblie;
								}else if(res.data.code==40001){
									that.$refs.inputDialog.open();
								}
							}
						})
					},
					fail: (res) => {
						console.log(res);
						that.$api.msg('绑定失败，请重试！')
					},
				});
				// #endif
			},
			dialogInputConfirm(value){
				let that = this;
				if(!value) return that.$api.msg('请输入推荐人ID');
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						that.$axios('login/bindmid','POST','member',{
							code: loginRes.code,
							agentid: value
						}).then(res=>{
							if(res.data.code == 200){
								uni.setStorageSync('token', res.data.data);
								uni.setStorageSync('Jh_recotabshops', 1);
								that.$refs.inputDialog.close();
								that.$refs.bind.close();
								uni.switchTab({
									url: '/pages/mallHome/selfMall'
								});
							}
						})
					},
					fail() {
						that.$api.msg('登录失败，请重试！')
					}
				})
				// #endif
				// #ifdef MP-ALIPAY
				my.getAuthCode({
					  scopes: 'auth_base',
					  success: res => {
						this.$axios('login/bindmidAlipay','POST','member',{
							code: res.authCode,
							agentid: value
						}).then(res=>{
							if(res.data.code == 200){
								uni.setStorageSync('token', res.data.data);
								uni.setStorageSync('Jh_recotabshops', 1);
								that.$refs.inputDialog.close();
								that.$refs.bind.close();
								uni.switchTab({
									url: '/pages/mallHome/selfMall'
								});
							}
						})
					}
				})
				// #endif
			},
			closeDialog(){
				this.$refs.inputDialog.close();
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f2f2f2;
	}
	.nobind{
		width: 600rpx;
		background: #fff;
		border-radius: 16rpx;
		text-align: center;
		padding: 60rpx 20rpx;
		font-size: 32rpx;
		button{
			margin-top: 40rpx;
		}
	}
	.c_userinfolist {
		padding: 0 20upx;
		background-color: #fff;
		display: flex;
		align-items: center;
		border-bottom: 1px solid #e8e8e8;
		border-top: 1px solid #e8e8e8;
		min-height: 40px;
		line-height: 40px;

		.c_userinfolistitem {
			width: 160upx;
		}

		input {
			width: 100%;
			font-size: 28upx;
			height: 40px;
			line-height: 40px;
		}
	}

	.validation {
		background-color: #ccc;
		text-align: center;
		position: absolute;
		right: 0;
		color: #333;
		z-index: 10;
		padding: 0 20upx;
		min-width: 210upx;
	}

	.title {
		line-height: 90upx;
		font-size: 32upx;
		padding: 0 20upx;
	}

	.btn {
		background-color: rgb(250, 67, 106);
		margin: 0 auto;
		margin-top: 36rpx;
		width: 94%;
		color: #fff;
		letter-spacing: 10upx;
	}

	.prompt {
		color: rgb(250, 67, 106);
		padding: 0 20upx;
		line-height: 50upx;
	}
	.nobind{
		button{
			background: #eee;
		}
	}
</style>
